//
new Vue({
    el: '#employee',
    data: {
        //
        showCalender: false, //日历
        showCTS: false, //选择时间弹层
        //
        //userid: '430563198902202051',
        userid:'',
        //员工信息
        userData: {
            all_count: '', //累计访问
            today_count: '', //今日访问
            avatar: '',
            name: '',
            position: ''
        },
        //
        browseTypes: [{
                name: '今天',
                type: '1'
            },
            {
                name: '昨天',
                type: '2'
            },
            {
                name: '本周',
                type: '3'
            },
            {
                name: '本月',
                type: '4'
            },
            {
                name: '更多',
                type: '5'
            }
        ],
        baIndex: '0',
        baType: '1',
        //浏览企业员工的数据
        browseLogArgs: {
            corp_userid: '', //员工企业用户id
            start_time: '', //开始时间
            end_time: '', //结束时间
            page: 1, //页码
            pageSize: 10, //每页条数
            type: '1' //1、今天，2、昨天，3、本周，4、本月，5、更多
        },
        toggleLEForCustomer: false,
        customerData: [],
        minDate: new Date(2017, 1, 1),
        maxDate: new Date(2023, 12, 31),
        showStartTime: false,
        showEndTime: false,
        startTime: new Date(),
        startTimeVal: '',
        endTime: new Date(),
        endTimeVal: '',
        mescroll: null //mescroll
    },
    created: function() {
        this.browseLogArgs.type = $.cookie("vmx-browseLogArgs-type20200628")?$.cookie("vmx-browseLogArgs-type20200628"):'1';
        this.baIndex = this.browseLogArgs.type*1 - 1;
        this.userid = window.location.search.slice(1).split('=')[1];
        console.log(this.userid);
    },
    computed: {
        showCustomerListEmpty() {
            return !this.customerData.length&&this.toggleLEForCustomer;
            //return false;
        }
    },
    mounted: function() {
        //
        fe.setRem();
        //
        fe.openLoad.afterImgsLoaded(function() {

        });
        //
        if(!this.userid){
            //vant.Toast.fail("");
            vant.Dialog.alert({
                title: '提示',
                message: '没有与企业员工对应的userid，请检查链接地址！',
            }).then(()=>{
                window.location.reload();
            });
            return
        }
        //
        this.mescroll = new MeScroll("body", { //id固定"body"
            down: {
                auto:false,
                callback: mescroll=>{
                    this.clearCustomerData();
                    this.browseLogArgs["page"] = 1;
                    //
                    this.getMyBrowseLog.bind(this)();
                }
            },
            //上拉加载的配置项
            up: {
                callback: this.getMyBrowseLog.bind(this), //上拉回调,此处可简写; 相当于 callback: function (page) { getListData(page); }
                noMoreSize: 5, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
                empty: {
                    //icon: "../res/img/mescroll-empty.png", //图标,默认null
                    tip: "~~暂无更多数据哦~~", //提示
                    //btntext: "去逛逛 >", //按钮,默认""
                    // btnClick: function(){//点击按钮的回调,默认null
                    // 	alert("点击了按钮,具体逻辑自行实现");
                    // } 
                },
                //clearEmptyId: "dataList", //相当于同时设置了clearId和empty.warpId; 简化写法;默认null; 注意vue中不能配置此项
                // toTop:{ //配置回到顶部按钮
                // 	src : "../res/img/mescroll-totop.png", //默认滚动到1000px显示,可配置offset修改
                // 	//html: null, //html标签内容,默认null; 如果同时设置了src,则优先取src
                // 	//offset : 1000
                // },
                lazyLoad: {
                    use: true
                }
            }
        });
        //
        this.getCorpUserForCorpWx().then(() => {
            return this.getVisitCount();
        }).catch(err => {
            vant.Toast.fail({
                message: '' + err
            });
        });
        //
        //vant.Toast('提示');
        // vant.Dialog({
        //   title: '标题',
        //   message: '弹窗内容',
        // });
        //
    },
    methods: {
        //打开访问详情
        openVisitDetail(info){
            //console.log(info);
            let _info = $.extend({
                visit_userid:this.userid
            },info)
            //console.log(_info);
            $.cookie("vmx-visiter-info20200628",JSON.stringify(_info));
            //
            $.cookie("vmx-browseLogArgs-type20200628",this.browseLogArgs.type);
            //console.log($.cookie("vmx-visiter-info20200628"));
            window.location.href = "./detail.html";
        },
        //获取员工个人信息
        getCorpUserForCorpWx() {
            return new Promise((resolve, reject) => {
                this.$http({
                    url: '/getCorpUserForCorpWx',
                    method: 'post',
                    data: {
                        userid: this.userid
                    }
                }).then(data => {
                    //console.log(data);
                    if (data.code == '1') {
                        $.each(data.body, (k, v) => {
                            this.$set(this.userData, k, v);
                        });
                        resolve();
                    } else {
                        reject(data.msg);
                    }
                }).catch(err => {
                    reject('获取员工个人信息失败');
                });
            });
        },
        //获取员工名片访问量
        getVisitCount() {
            return new Promise((resolve, reject) => {
                this.$http({
                    url: '/getVisitCount',
                    method: 'post',
                    data: {
                        userid: this.userid
                    }
                }).then(data => {
                    console.log(data);
                    if (data.code == '1') {
                        this.$set(this.userData, 'all_count', data.body.all_count);
                        this.$set(this.userData, 'today_count', data.body.today_count);
                        resolve();
                    } else {
                        reject(data.msg);
                    }
                }).catch(err => {
                    reject('获取员工名片访问量失败');
                });
            });
        },
        //获取与浏览过员工名片的客户数据
        getMyBrowseLog(info) {
            //console.log(info);
            this.$set(this.browseLogArgs, 'corp_userid', this.userid);
            return new Promise((resolve, reject) => {
                this.$http({
                    url: '/getMyBrowseLog',
                    method: 'post',
                    data: this.browseLogArgs
                }).then(data => {
                    console.log(data);
                    if (data.code == '1') {
                        if (data.body.data.length) {
                            //this.clearCustomerData();
                            //this.customerData = null;
                            data.body.data.forEach(item=>{
                                this.customerData.push(item)
                            });
                            this.browseLogArgs["page"]++;
                            //console.log(this.browseLogArgs);
                        }
                        if (!data.body.data.length && this.browseLogArgs.page == 1) {
                            this.toggleLEForCustomer = true
                        }
                        //
                        this.mescroll.endBySize(data.body.data.length, data.body.total); //必传参数(当前页的数据个数, 总数据量)
                        //
                        resolve();
                    } else {
                        vant.Toast.fail({
                            message: data.msg
                        });
                        reject(data.msg);
                    }
                }).catch(err => {
                    vant.Toast.fail({
                        message: '获取员工名片访问量失败'
                    });
                    reject('获取员工名片访问量失败');
                });
            });
        },
        //
        updateBrowseOfCall(info) {
            //console.log(info);
            return new Promise((resolve, reject) => {
                this.$http({
                    url: '/updateBrowseOfCall',
                    method: 'post',
                    data: {
                        visit_userid: this.userid, //被访者id
                        userid: info.userid //用户id
                    }
                }).then(data => {
                    console.log(data);
                    if (data.code == '1') {
                        resolve();
                    } else {
                        reject(data.msg);
                    }
                }).catch(err => {
                    reject('浏览记录改变联系状态失败');
                });
            });
        },
        //清空客户数据
        clearCustomerData() {
            this.customerData.splice(0, this.customerData.length);
        },
        //切换显示客户数据
        changeBATab(index, info) {
            if (this.baType == info.type && info.type != '5') return;
            this.baIndex = index;
            this.baType = info.type;
            this.browseLogArgs["page"] = 1;
            this.$set(this.browseLogArgs, 'type', info.type);
            //
            this.toggleLEForCustomer = false;
            this.clearCustomerData();
            if (info.type != '5') {
                this.$set(this.browseLogArgs, 'start_time', '');
                this.$set(this.browseLogArgs, 'end_time', '');
                this.mescroll.resetUpScroll();
            } else {
                this.showCTS = true;
            }
        },
        //时间选择
        openStartCalendar() {
            this.showStartTime = true;
        },
        cancelStartTime() {
            this.showStartTime = false;
        },
        confirmStartTime(date) {
            this.endTime = null;
            this.endTime = date;
            this.startTimeVal = this.dateFormatYYMMDD(date);
            this.$set(this.browseLogArgs, 'start_time', this.dateFormatYYMMDD2(date));
            this.showStartTime = false;
        },
        openEndCalendar() {
            this.showEndTime = true;
        },
        cancelEndtTime() {
            this.showEndTime = false;
        },
        confirmEndTime(date) {
            console.log(this.startTime);
            if (this.startTime.getTime() > date.getTime()) {
                vant.Toast('开始时间不能晚于结束时间');
                return;
            }
            this.endTimeVal = this.dateFormatYYMMDD(date);
            this.$set(this.browseLogArgs, 'end_time', this.dateFormatYYMMDD2(date));
            this.showEndTime = false;
            //
            this.getMyBrowseLog().then(() => {
                this.showCTS = false;
            });
        },
        resetTime() {
            this.startTime = null;
            this.startTime = new Date();
            this.endTime = null;
            this.endTime = new Date();
            this.startTimeVal = '';
            this.endTimeVal = '';
            this.$set(this.browseLogArgs, 'start_time', this.startTimeVal);
            this.$set(this.browseLogArgs, 'end_time', this.endTimeVal);
        }
    }
});
//
// 通过 CDN 引入时不会自动注册 Lazyload 组件
// 可以通过下面的方式手动注册
//Vue.use(vant);
//Vue.use(vant.Lazyload);